/*
 *  Copyright 2019-2023 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.biz.service.impl;

import me.zhengjie.modules.biz.domain.NewExamDict;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.biz.service.NewExamDictService;
import me.zhengjie.modules.biz.domain.vo.NewExamDictQueryCriteria;
import me.zhengjie.modules.biz.mapper.NewExamDictMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
 * @description 服务实现
 * @author yiwei.he
 * @date 2024-04-09
 **/
@Service
@RequiredArgsConstructor
public class NewExamDictServiceImpl extends ServiceImpl<NewExamDictMapper, NewExamDict> implements NewExamDictService {

    private final NewExamDictMapper newExamDictMapper;

    @Override
    public PageResult<NewExamDict> queryAll(NewExamDictQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(newExamDictMapper.findAll(criteria, page));
    }

    @Override
    public List<NewExamDict> queryAll(NewExamDictQueryCriteria criteria){
        return newExamDictMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(NewExamDict resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(NewExamDict resources) {
        NewExamDict newExamDict = getById(resources.getId());
        newExamDict.copy(resources);
        saveOrUpdate(newExamDict);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<NewExamDict> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (NewExamDict newExamDict : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("院校名称", newExamDict.getUname());
            map.put("专业名称", newExamDict.getPname());
            map.put("包含专业", newExamDict.getIncludeProfession());
            map.put("招考方向", newExamDict.getOrientation());
            map.put("选考科目要求", newExamDict.getCondition());
            map.put("层次【0：本科 1：专业】", newExamDict.getType());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}